package com.example.notice.news

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.NewsEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.notice.R
import com.example.notice.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/notice/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding,NewsIntent,NewsViewModel>() {
    val newsPageAdapter by lazy { NewsPageAdapter() }
    override fun getLayout(): Int  = R.layout.activity_news

    override fun getVM(): Class<NewsViewModel> = NewsViewModel::class.java

    override fun initView() {
        binding.newsRv.apply {
            adapter = newsPageAdapter
            layoutManager = LinearLayoutManager(this@NewsActivity)
        }
        newsPageAdapter.setOnItemClickLister(object :onItemClickLister{
            override fun onItemClick(position: Int, item: NewsEntityItem) {
                viewModel.sendIntent(NewsIntent.newsCancel(item.id))
            }
        })
    }

    override fun initData() {
            lifecycleScope.launch {
                viewModel.uiState.collect{
                    when(it){
                        is UiState.onSuccess<*> -> {
                            when(it.type){
                                HttpType.DEFAULT -> {
                                    val pagingData = it.data as PagingData<NewsEntityItem>
                                    newsPageAdapter.submitData(pagingData)
                                }
                                HttpType.UPDATE->{
                                    ToastUtils.showLong("撤销成功")
                                    viewModel.sendIntent(NewsIntent.news)
                                }
                                else->{}
                            }
                        }
                        else->{

                        }
                    }
                }
            }
    }

    override fun onResume() {
        super.onResume()
        viewModel.sendIntent(NewsIntent.news)
    }

}